<?php
include "header.php";
?>

<div class="container-fluid">
    <!--      <div class="row">-->
    <!--        <div class="col-sm-3 col-md-2 sidebar">-->
    <!--          <ul class="nav nav-sidebar">-->
    <!--            <li class="active">-->
    <!--                <a href="#">公云<span class="sr-only">(current)</span></a>-->
    <!--            </li>-->
    <!--            <li><a href="#">熙地港</a></li>-->
    <!--            <li><a href="#">望京凯德</a></li>-->
    <!--          </ul>-->
    <!---->
    <!--        </div>-->

    <div class="col-sm-12  col-md-12  main">

        <h1 class="page-header"><label id="lb_title_merchantname"></label>店铺信息
            <button id="btn_cancelCreateStore" type="button" class="btn btn-lg btn-warning pull-right" onclick="cancelCreateStore()" style="display: none">取消</button>
            <button id="btn_finish" type="button" class="btn btn-lg btn-success pull-right" onclick="finishCreateStore()" style="display: none">保存</button>
            <button id="btn_modify" type="button" class="btn btn-lg btn-success pull-right" onclick="finishModifyStore()" style="display: none">修改</button>
            <button id="btn_create" type="button" class="btn btn-lg btn-success pull-right" onclick="showCreateStoreDiv()">创建</button>
        </h1>
        <div style="padding: 100px 100px 10px;display: none" id="div_createStore">
            <form class="bs-example bs-example-form" role="form">
                <div class="input-group">
                    <span class="input-group-addon">所属商场</span>
                    <select id="select_merchant" class="cls_select2 form-control" name="state" onchange="getFloorSelectOption(this.value)">

                    </select>
                </div>
                <br />
                <div class="input-group">
                    <span class="input-group-addon">店铺名称</span>
                    <input id="input_name" type="text" class="form-control" placeholder="请输入店铺名称" >
                </div>
                <br>

                <div class="input-group">
                    <span class="input-group-addon">楼层</span>
<!--                    <input id="input_floor" type="text" class="form-control" placeholder="请输入楼层号">-->
                        <select id="select_floor" class="cls_select2 form-control" name="floor">

                        </select>
                </div>
                <br>
                <div class="input-group">
                    <span class="input-group-addon">面积</span>
                    <input id="input_area" type="text" class="form-control" placeholder="请输入面积(m²)">
                </div>
                <br>
                <div class="input-group">
                    <span class="input-group-addon">行业</span>

                    <select id="select_industry" class="cls_select2 form-control" name="industry" onchange="getIndustryOptionDetail(this.value)">

                    </select>

                    <select id="select_industrydetail" class="cls_select2 form-control" name="industrydetail">

                    </select>

                </div>
                <br>
            </form>
        </div>

        <div class="table-responsive">
            <table class="table table-striped " id="storeTable" >

            </table>
        </div>
    </div>

    <div class="row main" id="div_merchant_info">
        <div class="col-sm-6  col-md-6" style="font-size: larger">
            <div class="row">
                <label class="col-sm-3  col-md-3">商场名称：</label>
                <label class="col-sm-9  col-md-9" id="lb_merchantinfo_name">name</label>
            </div>
            <div class="row">
                <label class="col-sm-3  col-md-3">唯一标识：</label>
                <label class="col-sm-9  col-md-9" id="lb_merchantinfo_uuid">uuid</label>
            </div>
            <div class="row">
                <label class="col-sm-3  col-md-3">ID：</label>
                <label class="col-sm-9  col-md-9" id="lb_merchantinfo_id">id</label>
            </div>
            <div class="row">
                <label class="col-sm-3  col-md-3">地址：</label>
                <label class="col-sm-9  col-md-9" id="lb_merchantinfo_address">address</label>
            </div>

        </div>
    </div>
</div>
</div>




<script>
    tableurl ="api.php?method=getStoreTableByMerchantUUID&merchant_uuid=<?php echo $_REQUEST['merchant_uuid'];?>";

    $('.cls_select2').select2({width:"100%"});

    getMerchantInfo();
    showStoreTable();
    getMerchantsSelectByUserid();
    getIndustryOption();

    //$('.js-example-basic-single').select2();

    function showStoreTable()
    {
        //alert("showStoreTable");
        var $table = $('#storeTable');
        $table.bootstrapTable({
            //url: "merchantTable.json",
            url:tableurl,
            dataType: "json",
            pagination: true, //分页
            singleSelect: false,
            search: true, //显示搜索框
            pageSize:10,
            columns: [
                {
                    title: 'ID',
                    field: 'id',
                    align: 'left',
                    valign: 'middle',
                    sortable : true
                },
                {
                    title: '唯一标识',
                    field: 'uuid',
                    align: 'left',
                    valign: 'middle',
                    sortable : true
                },
                {
                    title: '店铺名',
                    field: 'name',
                    align: 'left',
                    valign: 'middle',
                    sortable : true
                },
                {
                    title: '业态',
                    field: 'industryname',
                    align: 'left',
                    valign: 'middle',
                    sortable : true
                },
                {
                    title: '楼层',
                    field: 'floorname',
                    align: 'left',
                    valign: 'middle',
                    sortable : true
                },
                {
                    title: '面积(m²)',
                    field: 'area',
                    align: 'left',
                    valign: 'middle',
                    sortable : true
                },
                {
                    title: '所属商场',
                    field: 'merchantname',
                    align: 'left',
                    valign: 'middle',
                    sortable : true
                },
                {
                    title: '操作',
                    field: 'id',
                    align: 'left',
                    formatter:function(value,row,index){

                        s = ' ';//<a href="#" mce_href="#" onclick="show(\''+ row.id + '\')">查看</a>

                        e = '<a href="#" mce_href="#" onclick="editS(\''+ row.id + '\',\''+ row.merchant_uuid + '\',\''+row.name+'\',\''+row.floor+'\',\''+row.area+'\',\''+row.industry+'\',\''+row.industrypid+'\')">编辑</a> ';

                        d = '<a href="#" mce_href="#" onclick="delS(\''+ row.id +'\')">删除</a> ';

                        return s+e+d;
                    }
                }
            ]
        });
    }
    current_modifystore_id=0;
    function editS(storeid,merchart_uuid,name,floor,area,industry,industrypid)
    {
        current_modifystore_id = storeid;
        $("#select_merchant").val(merchart_uuid).trigger("change");
        $("#input_name").val(name);
        $("#select_floor").val(floor).trigger("change");
        $("#input_area").val(area);

        if(industrypid==0)
        {
            $("#select_industry").val(industry).trigger("change");;
        }
        else
        {
            $("#select_industry").val(industrypid).trigger("change");
            $("#select_industrydetail").val(industry).trigger("change");
        }

        showModifyStoreDiv();
    }

    function getMerchantInfo()
    {
        $.ajax({
            url:'api.php?method=getMerchantInfoByMerchantUUID',
            type:'POST', //GET
            async:true,    //或false,是否异步
            data:{
                merchant_uuid:"<?php echo $_REQUEST['merchant_uuid'];?>"
            },
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                if(data.return_code=="0")
                {
                    $("#lb_title_merchantname").html(data.name+"的");
                    $("#lb_merchantinfo_name").html(data.name);
                    $("#lb_merchantinfo_uuid").html(data.uuid);
                    $("#lb_merchantinfo_id").html(data.id);
                    $("#lb_merchantinfo_address").html(data.address);
                }
                else
                {
                    $("#div_merchant_info").hide();
                    $("#lb_title_merchantname").html("<?php echo $_COOKIE['username'];?>可管理的");
                }



            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        })
    }

    function getMerchantsSelectByUserid()
    {
//        $('.js-example-basic-single').select2(
//            {
//                ajax: {
//                    url: 'api.php?method=getMerchantSelectByUserid',
//
//                    processResults: function (data) {
//                        // Tranforms the top-level key of the response object from 'items' to 'results'
//                        return {
//                            results: data
//                        };
//                    }
//
//                    }
//
//            }
//        );

        $.ajax({
            url:'api.php?method=getMerchantTableByUserid',
            type:'POST', //GET
            async:true,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                for(o in data)
                {
                    if(data[o].uuid==undefined)continue;
                    $("#select_merchant").append("<option value='" + data[o].uuid + "'>&nbsp;" + data[o].name + "</option>");

                }

                getFloorSelectOption($("#select_merchant").val());


            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });




    }

    function getIndustryOption()
    {
        $.ajax({
            url:'api.php?method=getIndustryOption',
            type:'POST', //GET
            async:true,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                for(o in data)
                {
                    if(data[o].id==undefined)continue;
                    $("#select_industry").append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                }

                getIndustryOptionDetail($("#select_industry").val());


            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });
    }

    function getIndustryOptionDetail(pid)
    {
        $.ajax({
            url:'api.php?method=getIndustryOptionDetail',
            type:'POST', //GET
            data:{
              pid:pid
            },
            async:false,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                $("#select_industrydetail").empty();
                for(o in data)
                {
                    if(data[o].id==undefined)continue;
                    $("#select_industrydetail").append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                }




            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });
    }

    function getFloorSelectOption(merchant_uuid)
    {
        $.ajax({
            url:'api.php?method=getFloorsByMerchantUUID',
            type:'POST', //GET
            data:{
                merchant_uuid:merchant_uuid
            },
            async:false,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                $("#select_floor").empty();
                for(o in data)
                {
                    if(data[o].id==undefined)continue;
                    $("#select_floor").append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                }



            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });
    }

    current_merchant="<?php echo $_REQUEST['merchant_uuid'];?>";

    function showCreateStoreDiv()
    {
        $("#div_createStore").show();
        $("#btn_cancelCreateStore").show();
        $("#btn_finish").show();
        $("#btn_create").hide();
        $(".table-responsive").hide();

        current_modifystore_id=0;

        if(current_merchant!="all")
        {
            $("#select_merchant").val(current_merchant).trigger("change");
        }

    }

    function cancelCreateStore()
    {
        $("#div_createStore").hide();
        $("#btn_cancelCreateStore").hide();
        $("#btn_finish").hide();
        $("#btn_modify").hide();
        $("#btn_create").show();
        $(".table-responsive").show();
    }

    function showModifyStoreDiv()
    {
        $("#div_createStore").show();
        $("#btn_cancelCreateStore").show();
        $("#btn_modify").show();
        $("#btn_create").hide();
        $(".table-responsive").hide();

    }

//    function cancelModifyStore()
//    {
//        $("#div_createStore").hide();
//        $("#btn_cancelCreateStore").hide();
//        $("#btn_modify").hide();
//        $("#btn_create").show();
//
//    }

    function checkStoreInput()
    {
        name = $("#input_name").val();
        storenum = $("#input_storenum").val();
        pronum = $("#input_pronum").val();
        floor = $("#select_floor").val();
        area = $("#input_area").val();
        industry=$("#select_industry").val();
        industrydetail=$("#select_industrydetail").val();
        if(name=="")
        {
            alert("店铺名称不能为空");
            return false;
        }
        else if(storenum=="")
        {
            alert("店铺编号不能为空");
            return false;
        }
        else if(pronum=="")
        {
            alert("商品编号不能为空");
            return;
        }
        else if(area=="")
        {
            alert("面积不能为空");
            return false;
        }
        //alert(industrydetail);
        if(industrydetail!=null)
        {
            industry = industrydetail;
        }
        return true;
    }

    function finishModifyStore()
    {
        bcheck=checkStoreInput();
        if(!bcheck)return;
        bootbox.confirm("确定修改此店铺的信息吗？",function (result) {

            if(result)
            $.ajax({
                url:'api.php?method=modifyStore',
                type:'POST', //GET
                async:true,    //或false,是否异步
                data:{
                    store_id:current_modifystore_id,
                    merchant_uuid:$("#select_merchant").val(),
                    name:name,
                    floor:floor,
                    area:area,
                    industry:industry
                },
                timeout:5000,    //超时时间
                dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                beforeSend:function(xhr){
                    console.log(xhr)
                    console.log('发送前')
                },
                success:function(data,textStatus,jqXHR){
                    //alert(data.name);
                    if(data.return_code=="0")
                    {
                        bootbox.alert("修改成功",function () {
                            cancelCreateStore();
                            $("#storeTable").bootstrapTable('refresh',{'url':tableurl});
                        });

                    }
                    else
                    {
                        alert(data.return_msg);
                    }



                },
                error:function(xhr,textStatus){
                    console.log('错误')
                    console.log(xhr)
                    console.log(textStatus)
                },
                complete:function(){
                    console.log('结束')
                }
            });
        })

    }

    function finishCreateStore()
    {
        bcheck=checkStoreInput();
        if(!bcheck)return;

        $.ajax({
            url:'api.php?method=addStore',
            type:'POST', //GET
            async:true,    //或false,是否异步
            data:{
                merchant_uuid:$("#select_merchant").val(),
                name:name,
                floor:floor,
                area:area,
                industry:industry
            },
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                if(data.return_code=="0")
                {
                    bootbox.alert("创建成功",function () {
                        cancelCreateStore();
                        $("#storeTable").bootstrapTable('refresh',{'url':tableurl});
                    });
                }
                else
                {
                    alert(data.return_msg);
                }



            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });

    }

    function delS(storeid)
    {
        bootbox.prompt("<font color='red'>确定要删除此店铺吗？</font>（此操作不可撤销！如需删除请在下方输入“确认删除”）",function (result) {
            if(result=="确认删除")
            {
                $.ajax({
                    url:'api.php?method=delStore',
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    data:{
                        store_id:storeid
                    },
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    beforeSend:function(xhr){
                        console.log(xhr)
                        console.log('发送前')
                    },
                    success:function(data,textStatus,jqXHR){
                        //alert(data.name);
                        if(data.return_code=="0")
                        {

                            bootbox.alert("删除成功",function () {
                                cancelCreateStore();
                                $("#storeTable").bootstrapTable('refresh',{'url':tableurl});
                            });
                        }
                        else
                        {
                            alert(data.return_msg);
                        }



                    },
                    error:function(xhr,textStatus){
                        console.log('错误')
                        console.log(xhr)
                        console.log(textStatus)
                    },
                    complete:function(){
                        console.log('结束')
                    }
                });
            }
        })
    }
</script>
</body>
</html>
